﻿Imports Negocio
Public Class frmTransferenciaPedidos

    Dim servicio1 As New BalanzaNegocio
    Dim empezar1 As Boolean = False

    Dim balanzaLogueada As String = "BAL001"

    Sub mostraMensaje(ByVal men As String)
        MsgBox(men, MsgBoxStyle.Information, "Mensajes al Usuario")
    End Sub

    Function verificarMarcados() As Boolean
        Dim valor As Boolean = False
        Dim contador As Integer = 0
        For Each fila As DataGridViewRow In dgvPedidos1.Rows
            If fila.Cells("Column3").Value = True Then
                contador = contador + 1
            End If
        Next
        If contador > 0 Then
            valor = True
        End If
        Return valor
    End Function

    Sub transferirPedidos()
        If verificarMarcados() = True Then
            For Each fila As DataGridViewRow In dgvPedidos1.Rows
                If fila.Cells("Column3").Value = True Then
                    servicio1.actualizarBalanza(fila.Cells("Column1").Value, cboBalanza2.SelectedValue.ToString)
                End If
            Next
            mostraMensaje("Actualización correcta.")
        Else
            mostraMensaje("Debe seleccionar al menos un pedido.")
        End If
    End Sub

    Sub listarPedidos1(balanza As String)
        Dim ds As DataSet = servicio1.obtenerPedidosBalanza(balanza, 2)
        dgvPedidos1.DataSource = ds.Tables(0)

        If ds.Tables(0).Rows.Count <> 0 Then
            dgvPedidos1.Rows(0).Selected = False
        End If
    End Sub

    Sub listarPedidos2(balanza As String)
        Dim ds As DataSet = servicio1.obtenerPedidosBalanza(balanza, 2)
        dgvPedidos2.DataSource = ds.Tables(0)

        If ds.Tables(0).Rows.Count <> 0 Then
            dgvPedidos2.Rows(0).Selected = False
        End If
    End Sub

    Sub cargarCombo1()
        With cboBalanza1
            .DataSource = servicio1.listarBalanzas("").Tables(0)
            .ValueMember = "BAL_Codigo"
            .DisplayMember = "BAL_Descripcion"
        End With
        empezar1 = True
    End Sub

    Sub cargarCombo2()
        With cboBalanza2
            .DataSource = servicio1.listarBalanzas(cboBalanza1.SelectedValue.ToString).Tables(0)
            .ValueMember = "BAL_Codigo"
            .DisplayMember = "BAL_Descripcion"
        End With
    End Sub

    Sub iniciar()
        dgvPedidos1.RowHeadersVisible = False
        cargarCombo1()
        listarPedidos1(balanzaLogueada)
        cargarCombo2()
    End Sub

    Private Sub frmTransferenciaPedidos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        iniciar()
    End Sub

    Private Sub cboBalanza1_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cboBalanza1.SelectedValueChanged
        If empezar1 = True Then
            cargarCombo2()
            balanzaLogueada = cboBalanza1.SelectedValue.ToString
            listarPedidos1(balanzaLogueada)
        End If
    End Sub

    Private Sub btnTransferir_Click(sender As System.Object, e As System.EventArgs) Handles btnTransferir.Click
        transferirPedidos()
    End Sub

    Private Sub cboBalanza2_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cboBalanza2.SelectedValueChanged
        If empezar1 = True Then
            listarPedidos2(cboBalanza2.SelectedValue.ToString)
        End If
    End Sub

End Class